package com.gadberry.utility.expression.function;

import java.util.List;

import com.gadberry.utility.algorithm.Algorithm;
import com.gadberry.utility.expression.Argument;
import com.gadberry.utility.expression.Expression;
import com.gadberry.utility.expression.Function;
import com.gadberry.utility.expression.InvalidArgumentsException;

/**
 * Converts each argument to a string then outputs the result to 
 * the console output. <br>
 * It does not add a carriage return after the last argument.
 * 
 * @author Peter Lager
 *
 */
public class PrintFunction extends Function {

	public PrintFunction(Expression expression) {
		super(expression);
	}

	@Override
	public Argument resolve(Algorithm algor) {
		List<Argument> args = getArguments();
		StringBuilder output = new StringBuilder();
		for (Argument arg : args) {
			output.append(arg.toString());
		}
		if(output.length() > 0)
			System.out.print(output.toString());
		return new Argument(new Boolean(false), getResolver());
	}

	@Override
	protected void checkArgs(List<Argument> args)
	throws InvalidArgumentsException {

		if (args.size() < 1) {
			throw new InvalidArgumentsException(
			"print requires al least 1 argument. No arguments provided.");
		}

		for (Argument arg : args) {
			if(arg.isNull()) {
				throw new InvalidArgumentsException(
				"Print cannot accept null arguments.  The argument provided is null.");
			} 
		}

	}

}
